<!DOCTYPE html>
<html lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>西安加油</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            position: relative;
            height: 100vh;
            background-image: linear-gradient(#b73e6e, #7a2f6f);
            font-family: Georgia, "Times New Roman", Times, serif;
            font-size: 14px;
            color: #fff;
            margin: 0;
            padding: 0;
            vertical-align: middle;
            display: flex;
            align-items: center;
            justify-content: center;
            perspective: 50vw;
            perspective-origin: 50% 50%;
        }

        .box {
            position: relative;
            width: 500px;
            height: 300px;
            background-color: #fff;
            box-shadow: 0px 10px 20px 20px rgba(0, 0, 0, 0.17);
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .card {
            position: relative;
            width: 185px;
            height: 260px;
            overflow: hidden;
            background-image: url(./img/xianrichu.jpg);
            background-repeat: no-repeat;
            background-position: 50% 50%;
            background-size: 110% 110%;
            transform-origin: 50% 50%;
            perspective: 1800px;
            transform-style: preserve-3d;
            border-radius: 8px;
            box-shadow: 0px 10px 20px 20px rgba(0, 0, 0, 0.17);
        }

        .card img {
            height: 40%;
            position: absolute;
            bottom: 5px;
            left: 80px;
        }

        .card p {
            text-align: center;
            margin-top: 50px;
            color: #fff700;
            font-size: 20px;
        }

        .tool {
            position: absolute;
            top: 0;
            left: 0;
            background: rebeccapurple;
            display: none;
        }

        .deng {
            position: relative;
            width: 120px;
            height: 90px;
            margin: 50px;
            background: #bb1314;
            border-radius: 50% 50%;
            transform-origin: 50% -100px;
            box-shadow: -5px 5px 50px 4px rgba(250, 108, 0, 1);
        }

        .deng:before {
            position: absolute;
            top: -7px;
            left: 29px;
            height: 12px;
            width: 60px;
            content: " ";
            display: block;
            z-index: 999;
            border-radius: 5px 5px 0 0;
            border: solid 1px #dc8f03;
            background: #ffa500;
        }

        .deng:after {
            position: absolute;
            bottom: -7px;
            left: 10px;
            height: 12px;
            width: 60px;
            content: " ";
            display: block;
            margin-left: 20px;
            border-radius: 0 0 5px 5px;
            border: solid 1px #dc8f03;
            background: #ffa500;
        }

        .line {
            position: absolute;
            top: -20px;
            left: 60px;
            width: 2px;
            height: 20px;
            background: #dc8f03;
        }

        .shui {
            width: 5px;
            height: 40px;
            background: #ffa500;
            border-radius: 0 0 5px 5px;
        }

        .shui-a {
            margin: -10px 0 0 40px;
            transform-origin: 50% -20px;
        }

        .shui-b {
            margin: -35px 0 0 59px;
            transform-origin: 50% -45px;
        }

        .shui-c {
            margin: -45px 0 0 77px;
            transform-origin: 50% -25px;
        }

        .deng-a {
            width: 100px;
            height: 90px;
            background: #d8000f;
            background: rgba(216, 0, 15, 0.2);
            margin: 0px 8px 8px 8px;
            border-radius: 50% 50%;
            border: 2px solid #dc8f03;
        }

        .deng-b {
            width: 45px;
            height: 90px;
            background: #d8000f;
            background: rgba(216, 0, 15, 0.2);
            margin: -4px 8px 8px 26px;
            border-radius: 50% 50%;
            border: 2px solid #dc8f03;
        }

        @media screen and (max-width: 400px) {
            .tool {
                display: block;
            }
        }
    </style>
</head>

<body>
    <div class="tool">
        <p>左右：<span id="alpha">0</span>
        </p>
        <p>前后：<span id="beta">0</span>
        </p>
        <p>扭转：<span id="gamma">0</span>
        </p>
    </div>

    <div class="box">
        <div class="deng cursor">
            <div class="line"></div>
            <div class="deng-a">
                <div class="deng-b">
                </div>
            </div>
            <div class="shui shui-a"></div>
            <div class="shui shui-b"></div>
            <div class="shui shui-c"></div>
        </div>

        <div class="card">
            <p>西安加油</p>
            <img src="./img/dyt.png" alt="大雁塔">
        </div>
    </div>
    <script src="./js/anime.min.js"></script>
    <script>
        anime({
            targets: ['.deng', '.shui-a', '.shui-b', '.shui-c'],
            rotate: ['0deg', '10deg', '-10deg'],
            duration: 3000,
            loop: true,
            easing: 'easeInOutQuad'
        });

        let clientWidth = document.body.clientWidth;
        let clientHeight = document.body.clientHeight;
        document.onmousemove = function (e) {
            e = e || window.event;
            let movex = 0;
            let movey = 0;
            if (e.pageX || e.pageY) {
                movex = e.pageX;
                movey = e.pageY
            }

            anime({
                targets: '.card',
                translateX: movex / 30 - 20,
                translateY: movey / 30 - 20,
                rotateX: (movey > clientHeight / 2 ? -1 : 1) * 5 + 'deg',
                rotateY: (movex > clientWidth / 2 ? 1 : -1) * 5 + 'deg',
                duration: 1000,
                easing: 'easeOutCirc'
            });

            anime({
                targets: '.card img',
                translateX: (movex - clientWidth / 2) / 30,
                translateY: (movey - clientHeight / 2) / 30,
                duration: 1000,
                easing: 'easeOutCirc'
            });

            anime({
                targets: '.card p',
                translateX: (movex - clientWidth / 2) / 30,
                translateY: (movey - clientHeight / 2) / 30,
                duration: 1000,
                easing: 'easeOutCirc'
            });
        }

        // 获取手机陀螺仪
        var updateGravity = function (event) {
            let x = Math.ceil(event.alpha * 100) / 100;
            let y = Math.ceil(event.beta * 100) / 100;
            let z = Math.ceil(event.gamma * 100) / 100;

            document.getElementById("alpha").innerHTML = x;
            document.getElementById("beta").innerHTML = y;
            document.getElementById("gamma").innerHTML = z;

            anime({
                targets: '.card',
                rotateX: (y > 0 ? 1 : -1) * 5 + 'deg',
                rotateY: (z > 0 ? 1 : -1) * 5 + 'deg',
                duration: 1000,
                easing: 'easeOutCirc'
            });


            anime({
                targets: '.card img',
                translateX: z % 180 / 5,
                translateY: y / 10,
                duration: 1000,
                easing: 'easeOutCirc'
            });

            anime({
                targets: '.card p',
                translateX: z % 180 / 5,
                translateY: y / 10,
                duration: 1000,
                easing: 'easeOutCirc'
            });

        };
        // 监听 window 的 deviceorientation 事件 
        window.addEventListener('deviceorientation', updateGravity, false);
    </script>
</body>

</html>